'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceAttendanceUserCtrl
 * @description
 * # AttendanceAttendanceUserCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('AttendanceAttendanceUserCtrl', function (DataService,Util, $rootScope, openApi, localStorageService, $routeParams) {
    var vm = this;
    var attDetail = localStorageService.get("attDetail")||{};
    this.peoplelist = [];
    $rootScope.userArr = []
    this.handleUser = function (userObj) {
      if (userObj.selected) {
        userObj.flag = 1
        $rootScope.userArr.push(userObj)
      } else {
        userObj.flag = 0
        $rootScope.userArr = $rootScope.userArr.filter(function (item, index) {
          return item.id != userObj.id
        })
      }
      $rootScope.$broadcast("userArrchange")
    }
    var init = function () {
      var param = {};
      param.acId = $routeParams.id;
      DataService.getData(openApi.attSelectExclusionn, param).then(function (data) {
        vm.peoplelist = data
        data.forEach(function (item, index) {
          item.children.forEach(function (item, index) {
            // 设置选中状态
            if (item.flag == 1) {
              item.selected = true;
              $rootScope.userArr.push(item)
            }
          });
          $rootScope.$broadcast("userArrchange")
        })

      })
    }
    this.confirm = function () {
      attDetail.exclusionUsers = $rootScope.userArr.map(function(item,index){
        return item.id
      });
      localStorageService.set("attDetail", attDetail);
      $rootScope.userArr = [];
      Util.goBack();
    }
    init()
  });
